package com.copm.demonstrationsystem.service.introductionfeatures.impl;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.config.SystemConfig;
import com.copm.demonstrationsystem.dao.commons.ImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.FacilityPageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionImageDao;
import com.copm.demonstrationsystem.pojo.commons.Image;
import com.copm.demonstrationsystem.pojo.introductionfeatures.BootPage;
import com.copm.demonstrationsystem.pojo.introductionfeatures.FacilityPage;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionImage;
import com.copm.demonstrationsystem.service.introductionfeatures.FacilityService;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.MultiImageAndTextHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.SinglonImageHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.TransformImagePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.List;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 15:11 2018/4/23
 */
@Service
public class FacilityServiceImpl implements FacilityService {

    private static Logger logger = LoggerFactory.getLogger(FacilityServiceImpl.class);

    @Autowired
    private SystemConfig config;

    @Autowired
    FacilityPageDao facilityPageDao;

    @Autowired
    SinglonImageHandle singlonImageHandle;

    @Autowired
    TransformImagePath transformImagePath;

    @Autowired
    ImageDao imageDao;

    //“周边设施”页面图片获取，默认获取所有图片
    @Override
    public ResponseStatus getFacilityImage() {
        ResponseStatus status = new ResponseStatus();
        FacilityPage facilityPage = facilityPageDao.findPage();
        Image image = facilityPage.getImage();
        if (null == image) {
            facilityPage.setImage(null);
        } else {
            transformImagePath.transformImagePath(image);
        }
        status.setStatue(1);
        status.setData(facilityPage.getImage());
        logger.info("Facility页面信息获取成功！ ");
        return status;
    }

    //“周边设施”页面图片上传
    @Override
    public ResponseStatus addFacilityImage(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (status.getStatue() == 0) {
            return status;
        }
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);

        //获取“周边设施”页面对象
        FacilityPage facilityPage = facilityPageDao.findPage();

        //设置“周边设施”页面图片
        facilityPage.setImage(image);
        //保存页面对象
        facilityPageDao.save(facilityPage);
        status.setStatue(1);
        logger.info("Facility页面图片上传成功！ ");
        return status;
    }

    //“周边设施”页面图片删除
    @Override
    public ResponseStatus deleteFacilityImage() {
        ResponseStatus status;
        //获取页面对象
        FacilityPage facilityPage = facilityPageDao.findPage();
        //获取图片名
        Image image = facilityPage.getImage();
        status = singlonImageHandle.deleteImage(image);
        if (status.getStatue() == 0) {
            return status;
        }
        //临时保存图片ID
        Long tempId = image.getId();
        //获取“周边设施”页面 ，设置图片为空
        facilityPage.setImage(null);
        //保存已删除的页面对象
        facilityPageDao.save(facilityPage);
        //从图片对象中删除页面所对应的图片数据
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info("Facility页面图片删除成功！ ");
        return status;
    }

    //“周边设施”页面图片修改
    @Override
    public ResponseStatus modifyFacilityImage(MultipartFile file) {
        ResponseStatus status;
        //查找页面
        FacilityPage facilityPage = facilityPageDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        if (null == facilityPage) {
            facilityPage = new FacilityPage();
            //保存对象
            facilityPageDao.save(facilityPage);
        }
        if (null != facilityPage.getImage()) {
            status = deleteFacilityImage();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addFacilityImage(file);
        logger.info("Facility页面图片修改成功！ ");
        return status;
    }

    @Override
    public ResponseStatus modifyFacilityToUrl(String toUrl) {
        ResponseStatus status = new ResponseStatus();
        //查询“周边设施”页面（只有一个页面数据）
        FacilityPage facilityPage = facilityPageDao.findPage();
        if (null == facilityPage) {
            //新建“周边设施”页面对象
            facilityPage = new FacilityPage();
        }
        //设置页面对象
        facilityPage.setNearToUrl(toUrl);
        //保存页面对象
        facilityPageDao.save(facilityPage);
        status.setStatue(1);
        logger.info("Facility页面 toUrl 修改成功！");
        return status;
    }


}
